<?php

namespace app\api\controller\chat;

use app\common\controller\ApiBasic;
use app\common\service\im\ChatlogService;
use app\common\service\sys\UserService;
use app\common\service\pmm\MessageService;
/**
 * 聊天接口
 */
class Chat extends ApiBasic
{
  // 初始化
  protected function initialize()
  {
    parent::initialize();
    // 当前控制器服务实例化
    $this->service = new ChatlogService();
  }

  public function getUserChatList()
  {
    if ($this->request->isGet()) {
      $data = $this->service->getUserChatList($this->user['id']);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  public function getUserChatObjList()
  {
    if ($this->request->isPost()) {
      $input = [
        'uid' => $this->user['id'],
        'rid' => input('rid/d', 0),
        'page' => input('page/d', 1),
        'limit' => input('limit/d', 20)
      ];
      $data = $this->service->getUserChatObjList($input);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  public function getUserGroupChatList()
  {
    if ($this->request->isPost()) {
      $input = [
        'uid' => $this->user['id'],
        'group_id' => input('group_id/d', 0),
        'page' => input('page/d', 1),
        'limit' => input('limit/d', 20)
      ];
      $data = $this->service->getUserGroupChatList($input);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  public function getinfobyid()
  {
    if ($this->request->isPost()) {
      $id  = input('id/d', 0);
      $data = (new UserService())->getInfoById($id);
      $data['avatar'] = _img($data['avatar']);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

   /**
     * 消息已读
     */
    public function hasread()
    {
        if ($this->request->isPost()) {
            $pms = new MessageService();
            $post = $this->request->post();
            $result = $pms->hasread($post, $this->user);
            return json($result);
        }
        return _jerr(1000);
    }
}
